import { ref, reactive } from 'vue'
import { msgOpen } from '@/utils/msg'

export function paging() {
    let datasArr = ref([]) //响应回来的结果（未分页前）
    let datasLength = ref(0) //数据行数
    let firstPage = ref(0) //起始页(当前页)
    let pageSize = ref(10) //每页行数
    let countPage = ref(0) //总页数
    let startIndex = ref(0) //数据截取起始位置
    let endIndex = ref(0) //数据截取最后位置
    let newDatasArr = ref([]) //截取后的新数据
    let btnDisabledObj = reactive({ //分页按钮禁用状态
        firstBtn: true,
        lastBtn: true,
        nextBtn: true,
        endBtn: true,
        firstInput: true,
        pageSizeInput: true
    })

    //分页前置处理
    /**
     * @param {Array} data 传入的数组对象
     */
    const pagingPreprocessing = (data) => {
        // console.log(data)
        if (data.length) {
            datasArr.value = data
            datasLength.value = data.length
            firstPage.value = !datasLength.value ? 0 : 1
            if (pageSize.value > datasLength.value && datasLength.value < 10) {
                pageSize.value = datasLength.value
            } else if (pageSize.value > datasLength.value && datasLength.value >= 10) {
                pageSize.value = 10
            } else if (pageSize.value < datasLength.value && datasLength.value < 10) {
                pageSize.value = datasLength.value
            } else if (pageSize.value < datasLength.value && datasLength.value >= 10) {
                pageSize.value = 10
            }
            countPage.value = !datasLength.value ? 0 : Math.ceil(datasLength.value / pageSize.value)
            startIndex.value = pageSize.value * (firstPage.value - 1)
            endIndex.value = startIndex.value + pageSize.value
            newDatasArr.value = datasArr.value.slice(startIndex.value, endIndex.value)
            if (countPage.value > 1) {
                btnDisabledObj.nextBtn = false
                btnDisabledObj.endBtn = false
                btnDisabledObj.firstInput = false
                btnDisabledObj.pageSizeInput = false
            } else if (countPage.value === 1) {
                btnDisabledObj.nextBtn = true
                btnDisabledObj.endBtn = true
                btnDisabledObj.lastBtn = true
                btnDisabledObj.firstBtn = true
                btnDisabledObj.firstInput = true
                btnDisabledObj.pageSizeInput = datasLength.value === 1 ? true : false
            } else if (!countPage.value) {
                btnDisabledObj.nextBtn = true
                btnDisabledObj.endBtn = true
                btnDisabledObj.lastBtn = true
                btnDisabledObj.firstBtn = true
                btnDisabledObj.firstInput = true
                btnDisabledObj.pageSizeInput = true
            }
        }else{
            newDatasArr.value = []
            btnDisabledObj.nextBtn = true
            btnDisabledObj.endBtn = true
            btnDisabledObj.lastBtn = true
            btnDisabledObj.firstBtn = true
            btnDisabledObj.firstInput = true
            btnDisabledObj.pageSizeInput = true
            firstPage.value = 0
            countPage.value = 0
            pageSize.value = 10
            datasLength.value = 0
            startIndex.value = 0
            endIndex.value = 0
        }
    }

    //下一页
    /**
     * @param {Number} pageNum 当前页
     */
    const nextPage = (pageNum) => {
        // console.log(pageNum);
        if (pageNum !== countPage.value && pageNum < countPage.value) {
            firstPage.value++
            if (firstPage.value === countPage.value) {
                btnDisabledObj.nextBtn = true
                btnDisabledObj.endBtn = true
                btnDisabledObj.lastBtn = false
                btnDisabledObj.firstBtn = false
            } else if (firstPage.value < countPage.value) {
                btnDisabledObj.nextBtn = false
                btnDisabledObj.endBtn = false
                btnDisabledObj.lastBtn = false
                btnDisabledObj.firstBtn = false
            }
            startIndex.value = pageSize.value * (firstPage.value - 1)
            endIndex.value = startIndex.value == 0 ? pageSize.value : startIndex.value + pageSize.value
            newDatasArr.value = datasArr.value.slice(startIndex.value, endIndex.value)
            // console.log(firstPage.value);
        }
    }

    //上一页
    /**
     * @param {Number} pageNum 当前页
     */
    const lastPage = (pageNum) => {
        // console.log(pageNum);
        if (pageNum !== 1) {
            firstPage.value--
            if (firstPage.value === 1) {
                btnDisabledObj.nextBtn = false
                btnDisabledObj.endBtn = false
                btnDisabledObj.lastBtn = true
                btnDisabledObj.firstBtn = true
            } else if (firstPage.value !== 1 && firstPage.value < countPage.value) {
                btnDisabledObj.nextBtn = false
                btnDisabledObj.endBtn = false
                btnDisabledObj.lastBtn = false
                btnDisabledObj.firstBtn = false
            }
            startIndex.value = pageSize.value * (firstPage.value - 1)
            endIndex.value = startIndex.value == 0 ? pageSize.value : startIndex.value + pageSize.value
            newDatasArr.value = datasArr.value.slice(startIndex.value, endIndex.value)
            // console.log(firstPage.value);
        }
    }

    //前往首页
    const toFirstPage = () => {
        firstPage.value = 1
        btnDisabledObj.nextBtn = false
        btnDisabledObj.endBtn = false
        btnDisabledObj.lastBtn = true
        btnDisabledObj.firstBtn = true
        startIndex.value = pageSize.value * (firstPage.value - 1)
        endIndex.value = startIndex.value == 0 ? pageSize.value : startIndex.value + pageSize.value
        newDatasArr.value = datasArr.value.slice(startIndex.value, endIndex.value)
    }

    //前往末页
    const toEndPage = () => {
        firstPage.value = countPage.value
        btnDisabledObj.nextBtn = true
        btnDisabledObj.endBtn = true
        btnDisabledObj.lastBtn = false
        btnDisabledObj.firstBtn = false
        startIndex.value = pageSize.value * (firstPage.value - 1)
        endIndex.value = startIndex.value == 0 ? pageSize.value : startIndex.value + pageSize.value
        newDatasArr.value = datasArr.value.slice(startIndex.value, endIndex.value)
    }

    //前往指定页
    /**
     * @param {Number} pageNum 去往指定页的页码
     */
    const toSpecifyPage = (pageNum) => {
        // console.log(pageNum);    
        if (pageNum < 1 || pageNum > countPage.value) {
            msgOpen("请输入正确的值")
        } else {
            firstPage.value = pageNum
            if (firstPage.value === 1) {
                btnDisabledObj.nextBtn = false
                btnDisabledObj.endBtn = false
                btnDisabledObj.lastBtn = true
                btnDisabledObj.firstBtn = true
            } else if (firstPage.value === countPage.value) {
                btnDisabledObj.nextBtn = true
                btnDisabledObj.endBtn = true
                btnDisabledObj.lastBtn = false
                btnDisabledObj.firstBtn = false
            } else if (firstPage.value !== 1 && firstPage.value !== countPage.value) {
                btnDisabledObj.nextBtn = false
                btnDisabledObj.endBtn = false
                btnDisabledObj.lastBtn = false
                btnDisabledObj.firstBtn = false
            }
            startIndex.value = pageSize.value * (firstPage.value - 1)
            endIndex.value = startIndex.value == 0 ? pageSize.value : startIndex.value + pageSize.value
            newDatasArr.value = datasArr.value.slice(startIndex.value, endIndex.value)
        }
    }

    //改变每页行数
    /**
     * @param {Number} pSize 修改每页显示行数
     */
    const changePageSize = (pSize) => {
        // console.log(pSize); 
        if (pSize > datasLength.value || pSize <= 0) {
            msgOpen("请输入正确的数量，不大于总行数！")
        } else if (pSize >= 100) {
            msgOpen("请输入小于100的值")
        } else {
            pageSize.value = pSize
            firstPage.value = 1
            btnDisabledObj.nextBtn = false
            btnDisabledObj.endBtn = false
            btnDisabledObj.lastBtn = true
            btnDisabledObj.firstBtn = true
            countPage.value = Math.ceil(datasLength.value / pageSize.value)
            startIndex.value = pageSize.value * (firstPage.value - 1)
            endIndex.value = startIndex.value + pageSize.value
            newDatasArr.value = datasArr.value.slice(startIndex.value, endIndex.value)
        }
    }
    return {
        datasArr,
        datasLength,
        firstPage,
        pageSize,
        countPage,
        newDatasArr,
        btnDisabledObj,
        pagingPreprocessing,
        nextPage,
        lastPage,
        toFirstPage,
        toEndPage,
        toSpecifyPage,
        changePageSize
    }
}

